package com.newbsd.web.maventest.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.newbsd.web.maventest.dao.UserDao;
import com.newbsd.web.maventest.dao.impl.UserDaoImpl;
import com.newbsd.web.maventest.pojo.User;


public class UserServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserDao userDao = new UserDaoImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String passwd = request.getParameter("password");

		User user = userDao.login(username, passwd);

		if (null == user) {

			request.setAttribute("msg", "login failed!");
			
			//request.getRequestDispatcher("/view/error.jsp").forward(request,
					//response);
			// 2
			// response.sendRedirect("/bookinfo/view/error.html");

		} else {
			// request.setAttribute("msg", "login success!");
			// request.getRequestDispatcher("/view/success.jsp").forward(request,
			// response);
			request.setAttribute("user", user);
			HttpSession se = request.getSession();
			se.setAttribute("username", username);
			se.setAttribute("user", user);
			request.getRequestDispatcher("User/Index.jsp").forward(request, response);
			//request.getRequestDispatcher("User/index2.jsp").forward(request, response);
		}

	}

}
